package com.chixing.day02;

import java.util.Arrays;

public class MyContainer {
    //容器初始容量为10
    public int[] defaultArray = new int[10];

    //容器已有元素的个数
    int size =0;

    /**
     *容器中添加新元素
     * @param ele 新元素
     */
    public void add(int ele){
        if (size < defaultArray.length){//不扩容
            defaultArray[size++] = ele;
        }else {//扩容
            int[] newArry = new int[defaultArray.length*2+1];
            for (int i=0;i<defaultArray.length;i++){
                newArry[i] = defaultArray[i];
            }
            newArry[size] = ele;
            size++;
            defaultArray = newArry;
        }
    }
    //遍历容器中的元素for
    public void show(){
        for (int i=0;i<size;i++){
            System.out.print(defaultArray[i]+",");
        }
    }
}

